package com.jiaoxn.example.common.response;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiaoxn.example.common.response.structure.ResponseStatusCodeEnum;
import com.jiaoxn.example.common.response.structure.ResponseStructure;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.Objects;

/**
 * @author jiao xn
 * @date 2022/8/27
 */
@RestControllerAdvice(basePackages = "com.jiaoxn.example")
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
    private final ObjectMapper objectMapper;

    @Autowired
    public ResponseAdvice(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body instanceof String) {
            return objectMapper.writeValueAsString(ResponseStructure.success(body));
        }

        if (body instanceof ResponseStructure) {
            int code = ((ResponseStructure<?>) body).getCode();
            response.setStatusCode(HttpStatus.valueOf(code));
            return body;
        }

        String requestMethod = Objects.requireNonNull(request.getMethod()).name();
        Integer responseCode;
        String responseMessage;

        switch (requestMethod) {
            case "POST", "UPDATE", "PATCH" -> {
                responseCode = ResponseStatusCodeEnum.UPDATE_RETURN_201.getCode();
                responseMessage = ResponseStatusCodeEnum.UPDATE_RETURN_201.getMessage();
            }
            case "DELETE" -> {
                responseCode = ResponseStatusCodeEnum.DELETE_RETURN_204.getCode();
                responseMessage = ResponseStatusCodeEnum.DELETE_RETURN_204.getMessage();
            }
            default -> {
                responseCode = ResponseStatusCodeEnum.SUCCESS.getCode();
                responseMessage = ResponseStatusCodeEnum.SUCCESS.getMessage();
            }
        }

        if (body instanceof String) {
            return objectMapper.writeValueAsString(ResponseStructure.success(responseCode, (String) body));
        }

        return ResponseStructure.success(responseCode, responseMessage, body);
    }
}
